
package com.cy.model;

import com.cy.BasicModel;

import com.cy.ext.plugin.tablebind.TableBind;

import java.util.Date;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-9-9
* Time: 下午12:56
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings("serial")
@TableBind(tableName="license_token",pkName = "token_id")
public class LicenseToken extends BasicModel<LicenseToken> {
    public static final LicenseToken dao = new LicenseToken();
    public static final String SELECT_ALL = "`token_id`,`license_token`,`user_id`,`num`,`type`,`status`,`license_log_id`,`remark`,`create_time`,`due_time` ";

    public enum Type{
        /*操作成功,返回*/
        ORDER(0, "订单生成"),
        COMPENSATION(1, "补尝生成"),
        ACTIVITY(1, "补尝生成");

        private Integer code;
        private String value;

        public Integer getCode() {
            return code;
        }

        public String getValue() {
            return value;
        }

        private Type(Integer code, String value) {
            this.code = code;
            this.value = value;
        }
    }

    public enum Status{
        /*操作成功,返回*/
        UNUSE(0, "末使用"),
        ISUSE(1, "已使用");

        private Integer code;
        private String value;

        public Integer getCode() {
            return code;
        }

        public String getValue() {
            return value;
        }

        private Status(Integer code, String value) {
            this.code = code;
            this.value = value;
        }
    }

    /**
     * 检查用户数据是否存在
     *
     * @param token
     * @return 是否存在
     */
    public boolean checkTokenUnique(String token) {
        boolean flag = false;

        LicenseToken licenseToken = findByToken(token);
        if (licenseToken != null) {
            flag = true;
        }
        return flag;
    }

    public LicenseToken findInfoByToken(String token){
        return findFirst("SELECT lt.*,co.phone ,co.user_id order_user_id FROM license_token lt,cy_order co WHERE co.`order_id` = lt.`order_id` AND license_token = ? ",token);
    }

    public LicenseToken findByToken(String token){
        return findFirst("select * from " + getTableName() + " where license_token = ?",token);
    }

    public List<LicenseToken> findByOrder(Long orderId){
        return find("select lt.* ,user.`nick_name`,user.`mobile`,user.`real_name` from " + getTableName() + " lt LEFT JOIN user ON lt.`user_id` = user.`user_id` where order_id = ?", orderId);
    }
    public List<LicenseToken> findByOrderNO(String orderNo){

        return find("SELECT co.order_no,co.`original_order_no`,co.`express_no`,co.`user_name`,co.`product_no`,lt.`token_id` license_token_id,lt.`license_token`,pack.color "
                +  " FROM package_info pack,cy_order co LEFT JOIN  license_token lt ON co.`order_id` = lt.`order_id` WHERE co.product_no = pack.pack_tag AND co.`order_no` = ?", orderNo);
    }



    public LicenseToken setTokenId (Long tokenId){
        this.set("token_id",tokenId);
        return this;
    }

    public Long getTokenId (){
        return this. get("token_id");
    }

        public LicenseToken setLicenseToken (String licenseToken){
        this.set("license_token",licenseToken);
        return this;
    }

    public String getLicenseToken (){
        return this.getStr("license_token");
    }

        public LicenseToken setUserId (Long userId){
        this.set("user_id",userId);
        return this;
    }

    public Long getUserId (){
        return this. get("user_id");
    }

        public LicenseToken setNum (Long num){
        this.set("num",num);
        return this;
    }

    public Long getNum (){
        return this. get("num");
    }

        public LicenseToken setType (Integer type){
        this.set("type",type);
        return this;
    }

    public Integer getType (){
        return this. get("type");
    }

        public LicenseToken setStatus (Integer status){
        this.set("status",status);
        return this;
    }

    public Integer getStatus (){
        return this. get("status");
    }

        public LicenseToken setLicenseLogId (Long licenseLogId){
        this.set("license_log_id",licenseLogId);
        return this;
    }

    public Long getLicenseLogId (){
        return this. get("license_log_id");
    }

        public LicenseToken setRemark (String remark){
        this.set("remark",remark);
        return this;
    }

    public String getRemark (){
        return this.getStr("remark");
    }

        public LicenseToken setCreateTime (Date createTime){
        this.set("create_time",createTime);
        return this;
    }

    public Date getCreateTime (){
        return this.getDate("create_time");
    }

        public LicenseToken setDueTime (Date dueTime){
        this.set("due_time",dueTime);
        return this;
    }

    public Date getDueTime (){
        return this.getDate("due_time");
    }

    public LicenseToken setOrderId (Long orderId){
        this.set("order_id",orderId);
        return this;
    }

    public Long getOrderId (){
        return this. get("order_id");
    }
}
